

package cn.liyao.web.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import cn.liyao.im.util.ConversationUtil;
import cn.liyao.tool.annotation.RequireToken;
import cn.liyao.tool.api.R;
import cn.liyao.web.constant.ConversationType;
import cn.liyao.web.constant.MessageTypeU;
import cn.liyao.web.holder.CurrentUserIdHolder;
import cn.liyao.web.model.*;
import cn.liyao.web.repository.*;
import io.netty.util.internal.StringUtil;
import lombok.SneakyThrows;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import server.processor.OnlineProcessor;
import server.protocal.Protocal;
import server.protocal.ProtocalFactory;
import server.utils.LocalSendHelper;

import javax.transaction.Transactional;
import java.util.*;
import java.util.stream.Collectors;

@RestController
public class GroupNoticeController {


    @Autowired
    private GroupNoticeRepository groupNoticeRepository;


    @Autowired
    private GroupMemberRepository groupMemberRepository;


    @Autowired
    private GroupRepository groupRepository;


    @Autowired
    private MessageRepository messageRepository;



    @Autowired
    private ConversationUtil conversationUtil;








    @SneakyThrows
    @GetMapping("/group/notice/create")
    @RequireToken()
    public R findUserById(String groupId,String content) {
        String userId = CurrentUserIdHolder.getCurrentUser();

        Optional<Group> groupOptional =  groupRepository.findById(groupId);
        if(!groupOptional.isPresent()) return R.fail("群组不存在");
        Group group = groupOptional.get();
        if(!group.getCreator().equals(userId)) return R.fail("只有群主才可以编辑");
        List<GroupMember> members =  groupMemberRepository.findAllByGroupIdAndUserState(groupId,0);
        if(members.size() == 0) return R.fail("群成员找不到");

        Map map = new HashMap();
        map.put("content",content);
        map.put("groupId", groupId);
        Protocal p = ProtocalFactory.createCommonData(JSONUtil.toJsonStr(map), "0", userId, true, null, MessageTypeU.noticeGroup.getValue());
        Messages messages = new Messages();
        BeanUtils.copyProperties(p,messages);
        messages.setFrom(userId);
        messages.setTo(groupId);
        messageRepository.save(messages);
        conversationUtil.createMessageStatus(messages);


        GroupNotice groupNotice = new GroupNotice();
        groupNotice.setContent(content);
        groupNotice.setFp(messages.getFp());
        groupNotice.setGroupId(groupId);
        groupNotice.setId(IdUtil.getSnowflakeNextIdStr());
        groupNotice.setTime(new Date());
        groupNoticeRepository.save(groupNotice);

        for(GroupMember groupMember : members){
            p.setTo(groupMember.getUserId());
            conversationUtil.createGroupConversation(groupId, groupMember.getUserId(),p);
            if(OnlineProcessor.isOnline(groupMember.getUserId())){
                LocalSendHelper.sendData(p,null);
            }
        }




        return R.data("修改成功");
    }

}